package custcare

import (
	"strconv"
	"wave-admin/global"
	ComReq "wave-admin/model/common/request"
	"wave-admin/model/common/response"
	"wave-admin/model/custcare/request"
	"wave-admin/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type VisitorApi struct{}

func (a *VisitorApi) VisitorList(ctx *gin.Context) {
	pageInfo := &request.VisitorList{}
	if err := ctx.ShouldBindQuery(pageInfo); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if err, list, total := visitorService.GetVisitorList(*pageInfo); err != nil {
		global.GnLog.Error("获取访客列表失败!", zap.Error(err))
		response.FailWithMessage(ctx, "获取访客列表失败"+err.Error())
	} else {
		response.Success(ctx, response.PageResult{
			List:        list,
			Total:       total,
			CurrentPage: pageInfo.CurrentPage,
			PageSize:    pageInfo.PageSize,
		}, "获取访客列表成功！")
	}
}

func (a *VisitorApi) Visitor(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	if err, visitor := visitorService.GetVisitor(id); err != nil {
		global.GnLog.Error("获取访客详情失败!", zap.Error(err))
		response.FailWithMessage(ctx, "获取访客详情失败"+err.Error())
	} else {
		response.Success(ctx, visitor, "获取访客详情成功！")
	}
}

func (a *VisitorApi) GetTalkList(ctx *gin.Context) {
	pageInfo := &request.VisitorTalk{}
	if err := ctx.ShouldBindQuery(pageInfo); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	userId := utils.GetUserID(ctx)
	if err, list := visitorService.GetTalk(*pageInfo, userId); err != nil {
		global.GnLog.Error("获取对话列表失败!", zap.Error(err))
		response.FailWithMessage(ctx, "获取对话列表失败"+err.Error())
	} else {
		response.Success(ctx, list, "获取对话列表成功！")
	}
}

func (a *VisitorApi) SetMsgRead(ctx *gin.Context) {
	req := &request.SetMsgRead{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	userId := utils.GetUserID(ctx)
	if err := visitorService.SetMsgRead(*req, userId); err != nil {
		global.GnLog.Error("设置对话已读失败!", zap.Error(err))
		response.FailWithMessage(ctx, "设置对话已读失败"+err.Error())
	} else {
		response.SuccessWithMessage(ctx, "设置对话已读成功！")
	}
}

func (a *VisitorApi) UpdateVisitorDetail(ctx *gin.Context) {
	visitorId, _ := strconv.Atoi(ctx.Param("id"))
	req := &request.ReqDetail{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if err := visitorService.UpdateVisitorDetail(*req, visitorId); err != nil {
		global.GnLog.Error("修改游客信息失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		response.SuccessWithMessage(ctx, "修改游客信息成功！")
	}
}

func (a *VisitorApi) VisitorIpBlack(ctx *gin.Context) {
	req := &request.VisitorIpBlack{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if err := visitorService.VisitorIpBlack(*req); err != nil {
		global.GnLog.Error("ip黑名单操作失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		msg := "加入IP黑名单成功！"
		if req.IsIpBlack == 0 {
			msg = "移除IP黑名单成功！"
		}
		response.SuccessWithMessage(ctx, msg)
	}
}

func (a *VisitorApi) GetUnreadMsgCount(ctx *gin.Context) {
	userId := utils.GetUserID(ctx)
	if err, unread := visitorService.GetUnreadMsgCount(userId); err != nil {
		global.GnLog.Error("获得未读消息数量失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		response.Success(ctx, unread, "获得未读消息数量成功！")
	}
}

func (a *VisitorApi) GetTrailList(ctx *gin.Context) {
	//visitorId, _ := strconv.Atoi(ctx.Param("id"))
	req := &request.TrailList{}
	if err := ctx.ShouldBindQuery(req); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if err, list := visitorService.GetTrailList(req.Id); err != nil {
		global.GnLog.Error("获得访客轨迹失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		response.Success(ctx, list, "获得访客轨迹成功！")
	}
}

func (a *VisitorApi) GetCustcareList(ctx *gin.Context) {
	pageInfo := &ComReq.PageInfo{}
	if err := ctx.ShouldBindQuery(pageInfo); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	userId := utils.GetUserID(ctx)
	if err, list, total := visitorService.GetCustcareList(*pageInfo, userId); err != nil {
		global.GnLog.Error("获取在线客服列表失败!", zap.Error(err))
		response.FailWithMessage(ctx, "获取在线客服列表失败"+err.Error())
	} else {
		response.Success(ctx, response.PageResult{
			List:        list,
			Total:       total,
			CurrentPage: pageInfo.CurrentPage,
			PageSize:    pageInfo.PageSize,
		}, "获取在线客服列表成功！")
	}
}

func (a *VisitorApi) TrailNewCustcare(ctx *gin.Context) {
	trail := &request.Trail{}
	if err := ctx.ShouldBindJSON(trail); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if err, data := visitorService.TrailNewCustcare(*trail); err != nil {
		global.GnLog.Error("转接到新的客服失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		response.Success(ctx, data, "转接到新的客服成功！")
	}
}
